/*
 * @Author: szx
 * @Date: 2021-11-15 11:35:48
 * @LastEditTime: 2021-11-15 11:49:05
 * @Description:
 * @FilePath: \leetcode\700-799\797\797.js
 */

/**
 * @param {number[][]} graph
 * @return {number[][]}
 */
var allPathsSourceTarget = function (graph) {
    const res = [];
    const stack = [];
    const n = graph.length - 1;
    const dfs = (graph, x) => {
        if (x === n) {
            res.push(stack.slice());
            return;
        }
        for (const y of graph[x]) {
            stack.push(y);
            dfs(graph, y);
            stack.pop();
        }
    };
    stack.push(0);
    dfs(graph, 0);
    return res;
};

var allPathsSourceTarget = function (graph) {
    const stack = [],
        res = [];
    const n = graph.length - 1;
    const dfs = (graph, x) => {
        if (x === n) {
            res.push(stack.slice());
            return;
        }
        for (const y of graph[x]) {
            stack.push(y);
            dfs(graph, y);
            stack.pop();
        }
    };

    stack.push(0);
    dfs(graph, 0);
    return res;
};
